package com.kj.xuyuan.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.kj.xuyuan.R;
import androidx.annotation.DrawableRes;

public class GlideUtils {

    private static MultiTransformation<Bitmap> commonTrans = new MultiTransformation<>(new CenterCrop(), new CircleCrop());

    public static void loadCircleImage(Context context, String url, ImageView imageView) {
        if (url == null) url = "";
        Glide.with(context).load(url)
                .apply(RequestOptions.bitmapTransform(commonTrans))
                .placeholder(R.mipmap.ic_error).error(R.mipmap.ic_error).into(imageView);
    }

    public static void loadCircleImageAddWhitStroke(Context context, String url, ImageView imageView) {
        if (url == null) url = "";
        Glide.with(context).load(url).centerCrop().placeholder(R.mipmap.ic_error)
                .transform(new GlideCircleTransform(context,2,context.getResources().getColor(R.color.white))).into(imageView);
    }

    public static void loadImage(Context context, Uri url, ImageView imageView) {
        Glide.with(context).load(url).centerCrop()
                .placeholder(R.mipmap.ic_error).error(R.mipmap.ic_error).into(imageView);
    }
   
    public static void loadImage(Context context, String url, ImageView imageView) {
        if (url == null) url = "";
        Glide.with(context).load(url).centerCrop()
                .placeholder(R.mipmap.ic_error).error(R.mipmap.ic_error).into(imageView);
    }

    public static void loadImage(Context context, String url, ImageView imageView, int defId) {
        if (url == null) url = "";
        Glide.with(context).load(url).centerCrop()
                .placeholder(defId).error(defId).into(imageView);
    }

    /**
     * 加载圆角图片
     *
     * @param context 上下文
     * @param view    图片控件
     * @param url     网络图片地址
     * @param radius  圆角度数
     * @param errorid 错误图片
     */

    public static void loadRoundImage(Context context, ImageView view, String url, int radius,
                                  @DrawableRes int errorid) {
        if (view == null) {
            return;
        }
        RequestOptions options = RequestOptions.bitmapTransform(new CenterCropRoundCornerTransform(radius));
        Glide.with(context)
                .load(url)
                .apply(options)
//                .placeholder(R.mipmap.ic_launcher)
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .error(errorid)
                .into(view);
    }




    public static Bitmap view2Bitmap(View view) {
        if (view == null) return null;
        Bitmap ret = Bitmap.createBitmap(
                view.getWidth(),
                view.getHeight(),
                Bitmap.Config.ARGB_8888
        );
        Canvas canvas = new Canvas(ret);
        Drawable bgDrawable = view.getBackground();
        if (bgDrawable != null) {
            bgDrawable.draw(canvas);
        } else {
            canvas.drawColor(Color.WHITE);
        }
        view.draw(canvas);
        return ret;
    }

}
